package br.usp.ime.servicosusp;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpRequestExecutor;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ViewJanus extends Activity {
	Button btn;
	WebView web;
	int Phase;
	String user;
	String pass;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_janus);
		btn = (Button) findViewById(R.id.btnLogin);
		btn.setOnClickListener(new btnLogin_Click());
		web = (WebView) findViewById(R.id.WebBrowser);
		web.getSettings().setJavaScriptEnabled(true);
		web.getSettings().setSaveFormData(false);
		web.getSettings().setSavePassword(false);
		Phase = 1;
		web.clearCache(true);
		web.clearFormData();
		web.clearHistory();	
		
		user = ViewSettings.loadSetting("UserJanus");
		pass = ViewSettings.loadSetting("PasswordJanus");

		web.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				afterLoaded();
			}

		});

	}

	private void afterLoaded() {
		if(Phase==1){
		web.loadUrl("javascript:document.getElementById('loginForm:usuario').value='"
				+ user + "';");
		web.loadUrl("javascript:document.getElementById('loginForm:senha').value='"
				+ pass + "';");
		web.loadUrl("javascript:document.getElementById('loginForm:login').click();");
		
		}else if(Phase==2){
			web.loadUrl("javascript:gotoAction('FichaDoAluno');");
		}
		
		Phase++;

	}

	private class btnLogin_Click implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			web.loadUrl("https://sistemas.usp.br/janus/comum/entrada.jsf");
		}
	}

	String fromStreamToString(InputStream is) {
		InputStreamReader isr = new InputStreamReader(is);
		StringWriter sw = new StringWriter();

		char[] buffer = new char[1024];

		try {
			int r;
			while ((r = isr.read(buffer)) != -1) {
				sw.write(buffer, 0, r);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return sw.toString();
	}
	
	private OptionsMenu optionsMenu;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		optionsMenu = new OptionsMenu(menu, this);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		optionsMenu.OptionsHandler(item);
		return true;
	}
}
